#!/bin/sh

# Slackware build script for mupdf-legacy

# Originally written by Hubert Hesse (email removed).
# Heavily modified by B. Watson (yalhcru@gmail.com).

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# 20200218 bkw: rename this build to -legacy, remove dynamic linking
#               and JavaScript support, install to /opt. Also get rid
#               of .desktop and icon.
# 20180623 bkw: BUILD=2, add patch for dynamic linking. Thanks
#               to Thomas Morper for pointing me in the right direction.
# 20180615 bkw: update for 1.13.0, move old change comments to ChangeLog.old
# 20180101 bkw: update for 1.12.0.
# 20170711 bkw: update for 1.11.
# 20170621 bkw: fix build with -current's newer openjpeg, BUILD=2.
# 20170122 bkw: update for 1.10a.

PRGNAM=mupdf-legacy
VERSION=${VERSION:-1.13.0}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

SRCNAM=mupdf

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi
set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $SRCNAM-$VERSION-source
tar xvf $CWD/$SRCNAM-$VERSION-source.tar.gz
cd $SRCNAM-$VERSION-source

# don't find/chown/chmod until after the thirdparty junk is removed.

# Build against system libs instead of bundled ones. If we didn't do this,
# libmupdf would be useless for building anything else.

# Everything removed from thirdparty/ has to be installed systemwide, and must
# either be a Slackware core pkg, or mentioned in REQUIRES= in the .info
# file... except mujs, which is mupdf's javascript support lib. If missing,
# we end up with a mupdf with no JS support, which is fine for use with
# zathura-pdf-mupdf and fbpdf use.

rm -rf thirdparty/*
sed -i 's,murun\.o,,' Makefile
sed -i 's,pdf_drop_js.*,;,' source/pdf/pdf-xref.c
sed -i '/murun_main/d' source/tools/mutool.c

chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

# ref https://bugs.ghostscript.com/show_bug.cgi?id=697175
sed -i '/#define *OPJ_STATIC/d' source/fitz/load-jpx.c

# Hard-code the version number so it'll stop trying to use git to retrieve
# it and spewing harmless but scary 'fatal: Not a git repository' errors.
sed -i "/^VERSION/s,=.*,= $VERSION," Makefile

make verbose=yes \
  XCFLAGS="$SLKCFLAGS -DJBIG_NO_MEMENTO" \
  HAVE_MUJS="no" \
  build=release \
  prefix=/opt/$PRGNAM \
  libdir=/opt/$PRGNAM/lib$LIBDIRSUFFIX \
  mandir=/opt/$PRGNAM/man \
  docdir=/usr/doc/$PRGNAM-$VERSION \
  DESTDIR=$PKG \
  all extra-apps install

# 1.12.0 used to install this stuff with 'make install', 1.13.0 doesn't:
for i in mjsgen muraster mupdf-x11-curl; do
  [ -e build/release/$i ] && install -s -m0755 build/release/$i $PKG/opt/$PRGNAM/bin
done

strip $PKG/opt/$PRGNAM/bin/*
gzip -9 $PKG/opt/$PRGNAM/man/man1/*.1

# Compatibility symlinks. Older versions, the binary is just "mupdf".
# Separate -curl binary exists in 1.5 but not 1.4.
ln -s $SRCNAM-x11 $PKG/opt/$PRGNAM/bin/$SRCNAM
ln -s $SRCNAM.1.gz $PKG/opt/$PRGNAM/man/man1/$SRCNAM-x11.1.gz
[ -e $PKG/opt/$PRGNAM/bin/$SRCNAM-x11-curl ] && \
  ln -s $SRCNAM.1.gz $PKG/opt/$PRGNAM/man/man1/$SRCNAM-x11-curl.1.gz

# mujstest is useless if we don't have JS support.
rm -f $PKG/opt/$PRGNAM/bin/mujstest

# Installed permissions are atrocious (everything 755).
find $PKG -type f | xargs chmod 644
chmod 755 $PKG/opt/$PRGNAM/bin/*

# .pc file taken from debian and parameterized.
PCLIBS="-l$SRCNAM -lfreetype -lharfbuzz -ljbig2dec -ljpeg -lcrypto -lopenjp2 -lz -lm"
PCLIBS="$PCLIBS -lmupdfthird"

mkdir -p $PKG/opt/$PRGNAM/lib$LIBDIRSUFFIX/pkgconfig/
sed -e "s,@LIB@,lib$LIBDIRSUFFIX,g" \
    -e "s,@VERSION@,$VERSION,g" \
    -e "s,@PCLIBS@,$PCLIBS,g" \
    $CWD/$SRCNAM.pc > $PKG/opt/$PRGNAM/lib$LIBDIRSUFFIX/pkgconfig/$SRCNAM.pc

# 'make install' already installed most of the docs.
cp -a CONTRIBUTORS $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-tgz}
